package stream;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * @author wang'ji'cheng
 * @date 2025/8/9
 */
public class MapDemo1 {
    public static void main(String[] args) {
        /*//使用map方法，对集合进行映射
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, "ab","abc");
        list.stream().map(s -> s.length()).toList().forEach(System.out::println);

        //提取字段存入新集合
        record User(Long id, String name) {

        }
        List<User> users = List.of(new User(1L, "tom"), new User(2L, "jack"));
        users.stream().map(user ->user.id).toList().forEach(System.out::println);*/

        //对象之间互转
        /*record OrderDO(Long id, BigDecimal price) {}
        record OrderDTO(Long id, BigDecimal price,Integer status) {}
        List<OrderDO> orderDOS = List.of(new OrderDO(1L, new BigDecimal("19.99")), new OrderDO(2L, new BigDecimal("20.99")));
        orderDOS.stream().map(Do->
           new OrderDTO(Do.id,Do.price , 0)
        ).toList().forEach(System.out::println);*/



        //复杂映射
        record Address(String city) {}
        record Person(String name, Address address) {}
        List<Person> people = List.of(
                new Person("Tom",   new Address("Beijing")),
                new Person("Jerry", null)
        );
        people.stream().map(p->Optional.ofNullable(p.address).map(a->a.city()).orElse("未知")).toList().forEach(System.out::println);
    }
}
